package com.example.leetcode.stack_deque;

import java.util.Stack;

/**
 * 翻转单词序列
 */
public class FliWord {


    public static void main(String[] args) {
        String input = "nowcoder. a am I";
        String s = ReverseSentence(input);
        System.out.println(s);

    }

    public static String ReverseSentence(String str) {
        if (str == null || str.equals("")) {
            return "";
        }

        Stack<String> stack = new Stack<>();
        String[] split = str.split(" ");
        for (String s : split) {
            stack.add(s);
        }
        int length = split.length;

        String res = "";
        while (!stack.isEmpty()) {
            length--;
            res += stack.pop();
            if (length != 0) {
                res += " ";
            }
        }

        return res;
    }


}
